' This program exported from BASIC Anywhere Machine (Version [5.2.3].[2023.12.16.16.28]) on 2024.05.19 at 20:57 (Coordinated Universal Time)
' This program by Charlie Veniot
' to test the DRAW statement and recursive subroutines.
' The program is in an endless loop, pausing for about 5 seconds before clearing the screen and drawing a new flower.
' On each loop, new random colours are chosen for each of the flower's six layers of petals.
' On each loop, the flower is drawn at a random degree of rotation.
' Note that when the flower is drawn at an angle other than zero, the low resolution imperfections become very noticeable.
' 
' Inspired by Richard Russell's "Flower-like tesselation" BBC BASIC program

m% = 178
SCREEN _NEWIMAGE(m% * 2,m% * 2,17)
DIM c%(1 TO 6), ainc%, astart%, aend%

DECLARE SUB Group1(x%,y%,a%,i%)
DECLARE SUB Group2(x%,y%,a%,i%)
DECLARE SUB Group3a(x%,y%,a%,i%)
DECLARE SUB Group3b(x%,y%,a%,i%)
DECLARE SUB Group4a(x%,y%,a%,i%)
DECLARE SUB Group4b(x%,y%,a%,i%)
DECLARE SUB Group4c(x%,y%,a%,i%)
DECLARE SUB Group5a(x%,y%,a%,i%)
DECLARE SUB Group5b(x%,y%,a%,i%)
DECLARE SUB Shape1(x%,y%,a%,c%)
DECLARE SUB Shape2(x%,y%,a%,c%)
DECLARE SUB Shape3(x%,y%,a%,c%)

DO 

  c%(6) = INT(RND*63) + 1
  c%(5) = INT(RND*63) + 1
  c%(4) = INT(RND*63) + 1
  c%(3) = INT(RND*63) + 1
  c%(2) = INT(RND*63) + 1
  c%(1) = INT(RND*63) + 1
  ainc% = INT(RND*45) 
  COLOR 0, 63
  CLS

  astart% = 0 + ainc% : aend% = 360 + ainc%
  FOR a% = astart% TO aend% STEP 45
     CALL Group1((m%+4),(m%+4),(a%),1)
  NEXT a%

  SLEEP 5
  LOOP

SUB Group1(x%,y%,a%,i%)
  DIM px%, py%
  DRAW "B M" + x% + "," + y%
  CALL Shape1((x%),(y%),(a%),c%(i%))
  DRAW "TA" + a% + " Z10 U10"
  IF i% <= 5 THEN
     px% = POINT(0) : py% = POINT(1)
     CALL Group1((px%),(py%),(a%),(i% + 1))
     DRAW "B M" + px% + "," + py%
  END IF
  DRAW "TA" + a% + " U10 W10 X10"
  IF i% > 1 AND i% <= 9 THEN
     px% = POINT(0) : py% = POINT(1)
     CALL Group2((px%),(py%),(a%),(i% + 1))
     DRAW "B M" + px% + "," + py%
  END IF
  DRAW "TA" + a% + " D10"
END SUB

SUB Group2(x%,y%,a%,i%)
  DIM px%, py%
  CALL Shape3((x%),(y%),(a%),c%(i%-1))
  DRAW "TA" + a% + " W10 U10 X10"
  IF i% = 4 THEN
     px% = POINT(0) : py% = POINT(1)
     CALL Group3a((px%),(py%),(a%),(i%))
     DRAW "B M" + px% + "," + py%
  END IF
  IF i% >= 5 THEN
     px% = POINT(0) : py% = POINT(1)
     CALL Group3b((px%),(py%),(a%),(i%))
     DRAW "B M" + px% + "," + py%
  END IF
  DRAW "TA" + a% + "D10"
END SUB

SUB Group3a(x%,y%,a%,i%)
  DIM px%, py%
  CALL Shape2((x%),(y%),(a%),c%(3))
  DRAW "TA" + a% + "W10 X10 D10"
END SUB

SUB Group3b(x%,y%,a%,i%)
  DIM px%, py%
  CALL Shape3((x%),(y%),(a%),c%(i%-1))
  DRAW "TA" + a% + " W10 U10 X10"
  IF i% = 5 THEN
     DRAW "TA" + a% + " D10"
     px% = POINT(0) : py% = POINT(1)
     CALL Group4a((px%),(py%),(a%),(i%))
     DRAW "B M" + px% + "," + py%
     DRAW "TA" + a% + "D10"
  ELSEIF i% = 6 THEN
     px% = POINT(0) : py% = POINT(1)
     CALL Group4b((px%),(py%),(a%),(i%))
     DRAW "B M" + px% + "," + py%
     DRAW "TA" + a% + "D20"
  ELSE
     px% = POINT(0) : py% = POINT(1)
     CALL Group4c((px%),(py%),(a%),(i%))
     DRAW "B M" + px% + "," + py%
     DRAW "TA" + a% + "D10"
  END IF
END SUB

SUB Group4a(x%,y%,a%,i%)
  CALL Shape2((x%),(y%),(a%),c%(4))
  DRAW "TA" + a% + " W10 X10 D10"
END SUB

SUB Group4b(x%,y%,a%,i%)
  DIM px%, py%
  CALL Shape2((x%),(y%),(a%),c%(5))
  DRAW "TA" + a% + " W10 X10 D10"
  px% = POINT(0) : py% = POINT(1)
  CALL Group5a((px%),(py%),(a%),(i%))
  DRAW "B M" + px% + "," + py%
  DRAW "TA" + a% + " D10"
END SUB

SUB Group4c(x%,y%,a%,i%)
  DIM px%, py%
  CALL Shape3((x%),(y%),(a%),c%(i%-1))
  DRAW "TA" + a% + " W10 U10 X10 D10"
  px% = POINT(0) : py% = POINT(1)
  CALL Group5B((px%),(py%),(a%),(i%))
  DRAW "B M" + px% + "," + py%
  DRAW "TA" + a% + " D10"
END SUB

SUB Group5a(x%,y%,a%,i%)
  CALL Shape2((x%),(y%),(a%),c%(5))
  DRAW "TA" + a% + " W10 X10 D10"
END SUB

SUB Group5b(x%,y%,a%,i%)
  DIM px%, py%
  CALL Shape2((x%),(y%),(a%),c%(6))
  DRAW "TA" + a% + " W10 X10 D10"
  px% = POINT(0) : py% = POINT(1)
  CALL Shape2((px%),(py%),(a%),c%(6))
  DRAW "B M" + px% + "," + py%
END SUB

SUB Shape1(x%,y%,a%,c%)
  DRAW "TA" + a% + " Z10 U20 W10 X10 D10 Y10 D10"
  DRAW "B TA" + a% + " U15"
  PAINT (POINT(0),POINT(1)),c%,0
  DRAW "B M" + x% + "," + y%
  SLEEP 0.025
END SUB

SUB Shape2(x%,y%,a%,c%)
  DRAW "TA" + a% + " W10 X10 D20 Y10 U10 Z10 U10"
  DRAW "B TA" + a% + " R5"
  PAINT (POINT(0),POINT(1)),c%,0
  DRAW "B M" + x% + "," + y%
  SLEEP 0.025
END SUB

SUB Shape3(x%,y%,a%,c%)
  DRAW "TA" + a% + " W10 U10 X10 D20 Y10 Z10 U10"
  DRAW "B TA" + a% + " R5"
  PAINT (POINT(0),POINT(1)),c%,0
  DRAW "B M" + x% + "," + y%
  SLEEP 0.025
END SUB